import urllib.request


# 使用处理器配置代理
BASE_URL = 'https://www.baidu.com/s?wd=ip'
# 伪装浏览器：
HEADER = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/103.0.0.0 Safari/537.36',
}

# 创建请求对象
request = urllib.request.Request(url=BASE_URL, headers=HEADER)

# 获取响应对象
# response = urllib.request.urlopen(request)

# 配置proxy参数
proxyParam = {
    'http': '111.72.218.180:9091'
}

# 获取处理器对象
handler = urllib.request.ProxyHandler(proxies=proxyParam)
opener = urllib.request.build_opener(handler)
response = opener.open(request)

# 得到响应数据
html = response.read().decode('UTF-8')

# 写入磁盘
fp = open(file='ip查询.html', mode='w', encoding='UTF-8')
fp.write(html)


